Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
71.43% |
5 / 7 |
CRAP | |
95.38% |
62 / 65 |
| ProductModelPropertiesNormalizer | |
0.00% |
0 / 1 |
|
71.43% |
5 / 7 |
17 | |
95.38% |
62 / 65 |
| __construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
| normalize | |
0.00% |
0 / 1 |
5 | |
97.30% |
36 / 37 |
|||
| getLabel | |
0.00% |
0 / 1 |
4.18 | |
77.78% |
7 / 9 |
|||
| supportsNormalization | |
100.00% |
1 / 1 |
2 | |
100.00% |
2 / 2 |
|||
| getAncestors | |
100.00% |
1 / 1 |
1 | |
100.00% |
5 / 5 |
|||
| getAncestorsIds | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
| getAncestorsCodes | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
| <?php | |
| declare(strict_types=1); | |
| namespace Akeneo\Pim\Enrichment\Component\Product\Normalizer\Indexing\ProductModel; | |
| use Akeneo\Pim\Enrichment\Component\Product\Model\ProductModelInterface; | |
| use Akeneo\Pim\Enrichment\Component\Product\Normalizer\Standard\Product\PropertiesNormalizer as StandardPropertiesNormalizer; | |
| use Akeneo\Pim\Enrichment\Component\Product\ProductAndProductModel\Query\CompleteFilterInterface; | |
| use Symfony\Component\Serializer\Normalizer\NormalizerInterface; | |
| use Symfony\Component\Serializer\SerializerAwareInterface; | |
| use Symfony\Component\Serializer\SerializerAwareTrait; | |
| /** | |
| * Transform the properties of a product model object (fields and product values) | |
| * to the indexing_product_model format. | |
| * | |
| * @author Nicolas Dupont <nicolas@akeneo.com> | |
| * @copyright 2017 Akeneo SAS (http://www.akeneo.com) | |
| * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) | |
| */ | |
| class ProductModelPropertiesNormalizer implements NormalizerInterface, SerializerAwareInterface | |
| { | |
| use SerializerAwareTrait; | |
| private const FIELD_FAMILY_VARIANT = 'family_variant'; | |
| private const FIELD_ID = 'id'; | |
| private const FIELD_PARENT = 'parent'; | |
| private const FIELD_ALL_INCOMPLETE = 'all_incomplete'; | |
| private const FIELD_ALL_COMPLETE = 'all_complete'; | |
| private const FIELD_ANCESTORS = 'ancestors'; | |
| /** @var CompleteFilterInterface */ | |
| private $completenessFilterQuery; | |
| public function __construct(CompleteFilterInterface $completenessFilterQuery) | |
| { | |
| $this->completenessFilterQuery = $completenessFilterQuery; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function normalize($productModel, $format = null, array $context = []) | |
| { | |
| if (!$this->serializer instanceof NormalizerInterface) { | |
| throw new \LogicException('Serializer must be a normalizer'); | |
| } | |
| $data = []; | |
| $data[self::FIELD_ID] = (string) $productModel->getId(); | |
| $data[StandardPropertiesNormalizer::FIELD_IDENTIFIER] = $productModel->getCode(); | |
| $data[StandardPropertiesNormalizer::FIELD_CREATED] = $this->serializer->normalize( | |
| $productModel->getCreated(), | |
| $format | |
| ); | |
| $data[StandardPropertiesNormalizer::FIELD_UPDATED] = $this->serializer->normalize( | |
| $productModel->getUpdated(), | |
| $format | |
| ); | |
| $family = null; | |
| $familyVariant = null; | |
| if (null !== $productModel->getFamilyVariant()) { | |
| $family = $this->serializer->normalize( | |
| $productModel->getFamilyVariant()->getFamily(), | |
| $format | |
| ); | |
| $familyVariant = $productModel->getFamilyVariant()->getCode(); | |
| } | |
| $data[StandardPropertiesNormalizer::FIELD_FAMILY] = $family; | |
| $data[self::FIELD_FAMILY_VARIANT] = $familyVariant; | |
| $data[StandardPropertiesNormalizer::FIELD_CATEGORIES] = $productModel->getCategoryCodes(); | |
| $parentCode = null !== $productModel->getParent() ? $productModel->getParent()->getCode() : null; | |
| $data[self::FIELD_PARENT] = $parentCode; | |
| $data[StandardPropertiesNormalizer::FIELD_VALUES] = !$productModel->getValues()->isEmpty() | |
| ? $this->serializer->normalize( | |
| $productModel->getValues(), | |
| ProductModelNormalizer::INDEXING_FORMAT_PRODUCT_MODEL_INDEX, | |
| $context | |
| ) : []; | |
| $normalizedData = $this->completenessFilterQuery->findCompleteFilterData($productModel); | |
| $data[self::FIELD_ALL_COMPLETE] = $normalizedData->allComplete(); | |
| $data[self::FIELD_ALL_INCOMPLETE] = $normalizedData->allIncomplete(); | |
| $data[self::FIELD_ANCESTORS] = $this->getAncestors($productModel); | |
| $data[StandardPropertiesNormalizer::FIELD_LABEL] = $this->getLabel( | |
| $data[StandardPropertiesNormalizer::FIELD_VALUES], | |
| $productModel | |
| ); | |
| return $data; | |
| } | |
| /** | |
| * Get label of the given product model | |
| * | |
| * @param array $values | |
| * @param ProductModelInterface $productModel | |
| * | |
| * @return array | |
| */ | |
| private function getLabel(array $values, ProductModelInterface $productModel): array | |
| { | |
| if (null === $productModel->getFamily()) { | |
| return []; | |
| } | |
| $attributeAsLabel = $productModel->getFamily()->getAttributeAsLabel(); | |
| if (null === $attributeAsLabel) { | |
| return []; | |
| } | |
| $valuePath = sprintf('%s-text', $attributeAsLabel->getCode()); | |
| if (!isset($values[$valuePath])) { | |
| return []; | |
| } | |
| return $values[$valuePath]; | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function supportsNormalization($data, $format = null) | |
| { | |
| return $data instanceof ProductModelInterface | |
| && ProductModelNormalizer::INDEXING_FORMAT_PRODUCT_MODEL_INDEX === $format; | |
| } | |
| /** | |
| * @param ProductModelInterface $productModel | |
| * | |
| * @return array | |
| */ | |
| private function getAncestors(ProductModelInterface $productModel): array | |
| { | |
| $ancestorsIds = $this->getAncestorsIds($productModel); | |
| $ancestorsCodes = $this->getAncestorsCodes($productModel); | |
| $ancestors = [ | |
| 'ids' => $ancestorsIds, | |
| 'codes' => $ancestorsCodes, | |
| ]; | |
| return $ancestors; | |
| } | |
| /** | |
| * @param ProductModelInterface $productModel | |
| * | |
| * @return array | |
| */ | |
| private function getAncestorsIds(ProductModelInterface $productModel): array | |
| { | |
| $ancestorsIds = []; | |
| while (null !== $parent = $productModel->getParent()) { | |
| $ancestorsIds[] = 'product_model_' . $parent->getId(); | |
| $productModel = $parent; | |
| } | |
| return $ancestorsIds; | |
| } | |
| /** | |
| * @param ProductModelInterface $productModel | |
| * | |
| * @return array | |
| */ | |
| private function getAncestorsCodes(ProductModelInterface $productModel): array | |
| { | |
| $ancestorsCodes = []; | |
| while (null !== $parent = $productModel->getParent()) { | |
| $ancestorsCodes[] = $parent->getCode(); | |
| $productModel = $parent; | |
| } | |
| return $ancestorsCodes; | |
| } | |
| } |